import AlertComponent from './Alert.vue';

// 初始化插件
const Alert = {};

Alert.install = function (Vue) {
  // 将Alert组件注册到全局的Vue上
  const AlertConstructor = Vue.extend(AlertComponent);
  // 初始化一个Alert实例
  const instance = new AlertConstructor();
  // 将组件实例绑定到一个div元素上
  instance.$mount(document.createElement('div'));
  // 将组件实例插入到body中
  document.body.append(instance.$el)

  // 4. 添加实例方法
  Vue.prototype.$alert = (msg) => {
    instance.type = 'alert';
    instance.message = msg;
    instance.isShow = true;
  }
  Vue.prototype.$confirm = (msg, success, cancel) => {
    instance.type = 'confirm';
    instance.message = msg;
    instance.isShow = true;
    if (success) {
      instance.success = success;
    }
    if (cancel) {
      instance.cancel = cancel;
    }
  }
}

export default Alert;